package com.easygoods.tasks;

import com.easygoods.service.OrderService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@CommonsLog
@Component
public class OrderSettleScheduling {
    @Autowired
    private OrderService orderService;

    //每天 0 点执行  秒 分 时 日 月 周,2个小时执行一次
    @Scheduled(fixedDelay = 2 * 3600 * 1000)
    public void settleOrders() {
        log.info("=======开始批量结算订单");
        List<String> orderIdList = this.orderService.getToSettleOrderIdList();
        for (String orderId : orderIdList) {
            log.info("=======开始结算订单 orderId:" + orderId);
            try {
                this.orderService.settle(orderId);
                log.info("=======结算订单成功 orderId:" + orderId);
            } catch (Exception e) {
                log.error("订单结算失败 orderId:" + orderId, e);
            }
        }
        log.info("=======完成批量结算订单");
    }
}
